package cn.iocoder.yudao.module.system.controller.admin.mini.userInfo.user.controller.admin.user;

import cn.iocoder.yudao.module.system.controller.admin.mini.other.shopGuide.dal.dataobject.shopguide.ShopGuideDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.shopGuide.dal.mysql.shopguide.ShopGuideMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnInfo.dal.dataobject.turninfo.TurnInfoDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnInfo.dal.mysql.turninfo.TurnInfoMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.userInfo.user.controller.admin.user.vo.UserPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.userInfo.user.dal.dataobject.user.UserDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.userInfo.user.service.user.QdUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 小程序-用户")
@RestController
@RequestMapping("/qd/user")
@Validated
public class QdUserController {

    @Resource
    private QdUserService userService;

//    @PostMapping("/create")
//    @Operation(summary = "创建小程序-用户")
//    @PreAuthorize("@ss.hasPermission('qd:user:create')")
//    public CommonResult<Integer> createUser(@Valid @RequestBody UserCreateReqVO createReqVO) {
//        return success(userService.createUser(createReqVO));
//    }

    @PutMapping("/update")
    @Operation(summary = "更新小程序-用户")
    @PreAuthorize("@ss.hasPermission('qd:user:update')")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody UserDO updateReqVO) {
        userService.updateUser(updateReqVO);
        return success(true);
    }

//    @DeleteMapping("/delete")
//    @Operation(summary = "删除小程序-用户")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('qd:user:delete')")
//    public CommonResult<Boolean> deleteUser(@RequestParam("id") Integer id) {
//        userService.deleteUser(id);
//        return success(true);
//    }

    @GetMapping("/get")
    @Operation(summary = "获得小程序-用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qd:user:query')")
    public CommonResult<UserDO> getUser(@RequestParam("id") Integer id) {
        return success(userService.getUser(id));
    }

//    @GetMapping("/list")
//    @Operation(summary = "获得小程序-用户列表")
//    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
//    @PreAuthorize("@ss.hasPermission('qd:user:query')")
//    public CommonResult<List<UserRespVO>> getUserList(@RequestParam("ids") Collection<Integer> ids) {
//        List<UserDO> list = userService.getUserList(ids);
//        return success(UserConvert.INSTANCE.convertList(list));
//    }

    @Resource
    ShopGuideMapper shopGuideMapper;
    @Resource
    TurnInfoMapper turnInfoMapper;

    @GetMapping("/page")
    @Operation(summary = "获得小程序-用户分页")
    @PreAuthorize("@ss.hasPermission('qd:user:query')")
    public CommonResult<PageResult<UserDO>> getUserPage(@Valid UserPageReqVO pageVO) {
        PageResult<UserDO> userPage = userService.getUserPage(pageVO);
        for (UserDO vo : userPage.getList()) {
            if (vo.getShopUserId()!=null){
                ShopGuideDO shopGuideDO = shopGuideMapper.selectById(vo.getShopUserId());
                if (shopGuideDO!=null){
                    vo.setShopName(shopGuideDO.getName());
                }
            }
            Integer byUid = turnInfoMapper.findByUid(vo.getId());
            vo.setTurnInfoSize(byUid);
        }
        return success(userPage);
    }

    @Operation(summary = "用户-抽奖记录")
    @GetMapping("/getUserTurn")
    public CommonResult getUserTurn(Integer uid){
        List<TurnInfoDO> turnInfoDOS = new ArrayList<>();
        if (uid!=null){
            turnInfoDOS=turnInfoMapper.selectList(new LambdaQueryWrapper<TurnInfoDO>().eq(TurnInfoDO::getUid, uid));
        }

        return success(turnInfoDOS);
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出小程序-用户 Excel")
//    @PreAuthorize("@ss.hasPermission('qd:user:export')")
//    @OperateLog(type = EXPORT)
//    public void exportUserExcel(@Valid UserExportReqVO exportReqVO,
//              HttpServletResponse response) throws IOException {
//        List<UserDO> list = userService.getUserList(exportReqVO);
//        // 导出 Excel
//        List<UserExcelVO> datas = UserConvert.INSTANCE.convertList02(list);
//        ExcelUtils.write(response, "小程序-用户.xls", "数据", UserExcelVO.class, datas);
//    }

}
